//剩菜
angular.module('SmartCanteenApp.proceduresAndWorkshop', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/proceduresAndWorkshop', {
            templateUrl: 'leftover/proceduresAndWorkshop.html',
            controller: 'proceduresAndWorkshopCtrl'
        });
    }])
    
    
	//剩菜管理
    .controller('proceduresAndWorkshopCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    	$(".modal-dialog").draggable();//为模态对话框添加拖拽
        $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动\n
        $(".select2").select2();
    	//获取所有车间
    	$scope.getWorkshops = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/workshops','','get',function(data){
				$scope.workshops = data.data
				console.log(data)
			})    		
    	}
    	//获取所有工序
    	$scope.getProcedure = function(){
    		$scope.getJson($scope.resultPath+'/wisdomeatery/v1/product_plan/canteens/'+$scope.canteenId+'/procedures','','get',function(data){
				$scope.procedures = data.data
				console.log(data)
			})
    	}
    	//获取所有食堂用户
    	$scope.getLeaders = function(){
    		$scope.getJson($scope.resultPath+'/wisdomeatery/v1/menus/canteens/'+$scope.canteenId+'/chief','','get',function(data){
				$scope.leaders = data.data
				console.log(data)
			})
    	}
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
				$scope.getProcedure()
				$scope.getWorkshops()
				$scope.getLeaders()
   		}
		$scope.$on('to-child', function(event,data) {
		   	console.log($scope.getId);
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			console.log($scope.canteenId);
			$scope.getProcedure()
			$scope.getWorkshops()
			$scope.getLeaders()
		});
    	//定义新增和编辑的车间对象
    	$scope.postWorkshopData = {
    			"name":"",
    			"id":"",
    			"leader_id":"",
    			"is_custom_board_worktime":false
    	}
    	$scope.postProcedureData = {
    			"name":"",
    			"id":""
    	}
    	//新建车间模态框弹出
    	$scope.newWorkshop = function(){
    		$scope.postWorkshopData = {
    			"name":"",
    			"id":"",
    			"leader_id":"",
    			"is_custom_board_worktime":false
    		}
    		$scope.method = '新建车间'
    		$('#putWorkshop').modal('toggle')
    	}
    	//新建工序模态框弹出
    	$scope.newProcedure = function(){
    		$scope.postProcedureData = {
    			"name":"",
    			"id":""
    		}
    		$scope.method = '新建工序'
    		$scope.isIntegerFlag = true
    		$('#putProcedure').modal('toggle')
    	}
    	//修改车间模态框弹出
    	$scope.editWorkshop = function(w){
    		$scope.postWorkshopData = {
    			"name":w.name,
    			"id":w.id,
    			"leader_id":w.leader_id,
    			"is_custom_board_worktime":w.is_custom_board_worktime
    		}
    		$scope.method = '修改车间'
    		
    		$('#putWorkshop').modal('toggle')
    	}
    	//修改工序模态框弹出
    	$scope.editProcedure = function(p){
    		$scope.postProcedureData = {
    			"name":p.name,
    			"id":p.id,
    			"intervalTime":p.intervalTime
    		}
    		$scope.method = '修改工序'
    		$('#putProcedure').modal('toggle')
    		$scope.isIntegerFlag = true
    		console.log($scope.isIntegerFlag)
    	}
    	//执行新建&修改工序
    	$scope.saveProcedure = function(){
    		//执行新建
    		if($scope.postProcedureData.id == ''||$scope.postProcedureData.id == undefined){
    			delete $scope.postProcedureData.id
    			var intervalTime = $scope.postProcedureData.intervalTime
				$scope.isIntegerFlag = isInteger(intervalTime)
				if($scope.isIntegerFlag){
		  			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/procedures/'+$scope.canteenId,$scope.postProcedureData,'post',function(data){
		  				if(data.code == 0){
		  					$scope.postProcedureData = {
					    			"name":"",
					    			"id":"",
									"intervalTime":0
				    			}
		  					$scope.getProcedure()
		  				}else{
		  					$scope.showError(data.msg)
		  				}
		  			})
		  			$('#putProcedure').modal('toggle')
    			}
    		}
    		//执行修改
    		else{ 
    			let id =  $scope.postProcedureData.id
    			delete $scope.postProcedureData.id
    			console.log($scope.postProcedureData)
    			var intervalTime = $scope.postProcedureData.intervalTime
//  			parseInt(intervalTime)
				$scope.isIntegerFlag = isInteger(intervalTime)
    			if($scope.isIntegerFlag){
    				console.log($scope.postProcedureData)
	      			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/procedures/'+id,$scope.postProcedureData,'put',function(data){
	    				if(data.code == 0){
	    					$scope.getProcedure()
	    				}else{
	    					$scope.showError(data.msg)
	    				}
	    			})
					$('#putProcedure').modal('toggle')
    			}
    		}
    		
    	}
    	//执行新建&修改车间
    	$scope.saveWorkshop = function(){
    		if($scope.postWorkshopData.id == ''||$scope.postWorkshopData.id == undefined){
    			delete $scope.postWorkshopData.id
    			console.log($scope.postWorkshopData)
    			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/workshops/'+$scope.canteenId,$scope.postWorkshopData,'post',function(data){
    				if(data.code == 0){
    					$scope.postWorkshopData = {
			    			"name":"",
			    			"id":"",
    						"leader_id":"",
    						"is_custom_board_worktime":false
		    			}
    					$scope.getWorkshops()
    				}else{
    					$scope.showError(data.msg)
    				}
    			})
    			
    		}
    		//执行修改
    		else{ 
    			let id =  $scope.postWorkshopData.id
    			delete $scope.postWorkshopData.id
    			console.log($scope.postWorkshopData)
    			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/workshops/'+id,$scope.postWorkshopData,'put',function(data){
    				if(data.code == 0){
    					$scope.getWorkshops()
    				}else{
    					$scope.showError(data.msg)
    				}
    			})
    		}
    		$('#putWorkshop').modal('toggle')
    	}
    	//删除车间
    	$scope.delWorkshop = function(data){
    		$scope.delName = '车间'+data.name
    		$scope.delId = data.id
    		$scope.delFlag = 'workshop'
    		$('#delData').modal('toggle')
    	}
    	//删除工序
    	$scope.delProcedure = function(data){
    		$scope.delName = '工序'+data.name
    		$scope.delId = data.id
    		$scope.delFlag = 'procedure'
    		$('#delData').modal('toggle')
//			console.log($scope.delName)
    	}
    	//确定删除车间&工序
    	$scope.sureDel  = function(){
    		if($scope.delFlag == 'workshop'){
    			console.log($scope.delId)
    			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/workshops/'+$scope.delId,'','delete',function(data){
    				if(data.code == 0){
    					$scope.getWorkshops()
    				}else{
    					$scope.showError(data.msg)
    				}
    			})
    			$('#delData').modal('toggle')
    		}else if($scope.delFlag == 'procedure'){
    			console.log($scope.delId)
    			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/procedures/'+$scope.delId,'','delete',function(data){
    				if(data.code == 0){
    					$scope.getProcedure()
    				}else{
    					$scope.showError(data.msg)
    				}
    			})
    			$('#delData').modal('toggle')
    		}
    	}
    	
    }])
